﻿using System;
using UnityEngine;
using UnityEngine.UI;

namespace Simulation.Framework
{
    public static class Extensions
    {
        public static T GetOrAddComponent<T>(this GameObject go) where T : Component
        {
            T t = go.GetComponent<T>();
            if (t == null)
                return go.AddComponent<T>();
            else
                return t;
        }

        public static void SetLayerAllChildren(this GameObject go, int layer)
        {
            Transform[] transforms = go.GetComponentsInChildren<Transform>();
            for (int i = 0; i < transforms.Length; i++)
            {
                GameObject g = transforms[i].gameObject;
                g.layer = layer;
            }
        }

        /// <summary>
        /// 除法
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public static Vector2 ExceptOperator(this Vector2 v1, Vector2 v2)
        {
            v1.x /= v2.x;
            v1.y /= v2.y;
            return v1;
        }

        /// <summary>
        /// 除法
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public static Vector3 ExceptOperator(this Vector3 v1, Vector3 v2)
        {
            v1.x /= v2.x;
            v1.y /= v2.y;
            v1.z /= v2.z;
            return v1;
        }

        /// <summary>
        /// 乘法
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public static Vector2 MultiplicationOperator(this Vector2 v1, Vector2 v2)
        {
            v1.x *= v2.x;
            v1.y *= v2.y;
            return v1;
        }

        /// <summary>
        /// 乘法
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        /// <returns></returns>
        public static Vector3 MultiplicationOperator(this Vector3 v1, Vector3 v2)
        {
            v1.x *= v2.x;
            v1.y *= v2.y;
            v1.z *= v2.z;
            return v1;
        }

        /// <summary>
        /// 图片自适应
        /// </summary>
        public static void ImageAdaption(this Image img, RectTransform viewPort,Texture2D tex)
        {
            if (tex == null) return;

            RectTransform imageRect = img.rectTransform;

            // image
            float texRadio = (float)tex.width / tex.height;
            float itemRadio = viewPort.rect.width / viewPort.rect.height;

            if (texRadio > itemRadio)
            {
                float width = viewPort.rect.width;
                float height = width / texRadio;
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
            }
            else
            {
                float height = viewPort.rect.height;
                float width = height * texRadio;
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
            }
        }

        /// <summary>
        /// 图片自适应，裁剪
        /// </summary>
        /// <param name="img"></param>
        /// <param name="viewPort"></param>
        /// <param name="tex"></param>
        public static void ImageAdaptionInverse(this Image img, RectTransform viewPort, Texture2D tex)
        {
            if (tex == null) return;

            RectTransform imageRect = img.rectTransform;

            // image
            float texRadio = (float)tex.width / tex.height;
            float itemRadio = viewPort.rect.width / viewPort.rect.height;

            if (texRadio > itemRadio)
            {
                float height = viewPort.rect.height;
                float width = height * texRadio;
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
            }
            else
            {
                float width = viewPort.rect.width;
                float height = width / texRadio;
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
                imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
            }
        }


        // 获取节点路径
        public static string GetRoute(this Transform transform, string splitter = "/")
        {
            string result = transform.name;
            Transform parent = transform.parent;
            while (parent != null)
            {
                result = $"{parent.name}{splitter}{result}";
                parent = parent.parent;
            }
            return result;
        }
    }
}
